package com.bawei.module_message.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_message.R
import com.bawei.module_message.databinding.ItemMessageBinding
import com.bawei.module_message.model.MessageModelItem

class MessageAdapter():RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    val list=mutableListOf<MessageModelItem>()

    fun setData(data:List<MessageModelItem>){
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val binding=ItemMessageBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return MessageViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val binding=DataBindingUtil.getBinding<ItemMessageBinding>(holder.itemView) ?:return
        binding.messageTitle.text=list[position].messageType
        binding.messageContent.text=list[position].messageContent
        binding.messageStatus.text=list[position].messageStatus
        binding.messageTime.text=list[position].sendTime
        if(list[position].messageStatus.equals("已读")){
            binding.messageIv.setImageResource(R.mipmap.messageimg6)
            binding.messageStatus.background=ContextCompat.getDrawable(binding.root.context,R.drawable.message_status)
        }else if(list[position].messageStatus.equals("处理中")){
            binding.messageIv.setImageResource(R.mipmap.messageimg7)
            binding.messageStatus.background=ContextCompat.getDrawable(binding.root.context,R.drawable.message_status1)
        }else{
            binding.messageIv.setImageResource(R.mipmap.messageimg8)
            binding.messageStatus.background=ContextCompat.getDrawable(binding.root.context,R.drawable.message_status2)
        }


    }

    class MessageViewHolder(item:ItemMessageBinding):RecyclerView.ViewHolder(item.root)
}